### Pilot survey replication analysis (presented in SI)
### Replication Package associated with Perspectives on Politics article
### "How Police Behavior Shapes Perceptions of Protests: Evidence from Black Lives Matter"
### Jasmine English, Ariel White, Laurel Eckhouse

## Setting up ----

# Load packages
library(dplyr)
library(psych)
library(data.table)
library(xtable)
library(stats)

#Read in data
full <- read.csv(file = "ProtestSurvey1_2019-11-04-with randomizer-flagged_cleaned.csv", header = T, stringsAsFactors = F) 

## Setting up variables ----

full$treat <- ifelse(full$FL_10_DO=="Treatment1", 1, 0)

full$violent <- factor(full$o_violent, levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"), ordered=T)
full$violentscale <- recode(full$violent, "Strongly disagree"=1,"Somewhat disagree"=2,"Neither agree nor disagree"=3,"Somewhat agree"=4,"Strongly agree"=5)
violent_t <- t.test(full$violentscale ~ full$treat) #direction is opposite the flat t-test because of how I set up the scale.

full$trouble <- factor(full$o_trouble, levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"), ordered=T)
full$troublescale <- recode(full$trouble, "Strongly disagree"=1,"Somewhat disagree"=2,"Neither agree nor disagree"=3,"Somewhat agree"=4,"Strongly agree"=5)
trouble_t <- t.test(full$troublescale ~ full$treat)

full$listen <- factor(full$o_listen, levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"), ordered=T)
full$listenscale <- recode(full$listen, "Strongly disagree"=1,"Somewhat disagree"=2,"Neither agree nor disagree"=3,"Somewhat agree"=4,"Strongly agree"=5)
listen_t <- t.test(full$listenscale ~ full$treat)

full$justified <- factor(full$o_justified, levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"), ordered=T)
full$justifiedscale <- recode(full$justified, "Strongly disagree"=1,"Somewhat disagree"=2,"Neither agree nor disagree"=3,"Somewhat agree"=4,"Strongly agree"=5)
justified_t <- t.test(full$justifiedscale ~ full$treat)

full$involved <- factor(full$o_involved, levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"), ordered=T)
full$involvedscale <- recode(full$involved, "Strongly disagree"=1,"Somewhat disagree"=2,"Neither agree nor disagree"=3,"Somewhat agree"=4,"Strongly agree"=5)
involved_t <- t.test(full$involvedscale ~ full$treat)

full$close <- factor(full$o_close, levels=c("Not At All Close","Not Too Close","Somewhat Close","Very Close"), ordered=T)
full$closescale <- recode(full$close, "Not At All Close"=1,"Not Too Close"=2,"Somewhat Close"=3,"Very Close"=4)
close_t <- t.test(full$closescale ~ full$treat)

full$gotoprotest <- as.numeric(full$o_actions_1); summary(full$gotoprotest)
gotoprotest_t <- t.test(full$gotoprotest ~ full$treat)

full$socialmedia <- as.numeric(full$o_actions_2); summary(full$gotoprotest)
socialmedia_t <- t.test(full$socialmedia ~ full$treat)

## Table A4 : Outcome measures and differences-of-means from pilot study (adjusted p-values) ----

questionlabels <- c("The protesters had violent intentions", "These protesters were out to cause trouble", "It is important to listen to these protesters","The protesters' actions were justified","I would consider getting involved with a group who supported causes similar to those of the protesters", "On the following scale, how close are your beliefs to those of the protesters you just read about?", "How likely would you be to take the following actions? Go to a protest like this one","How likely would you be to take the following actions? Post something positive about a protest like this on social media")
questionscales <- c("5-pt Likert agreement (Strongly disagree, somewhat disagree, neither agree nor disagree, somewhat agree, strongly agree)", "5-pt Likert agreement", "5-pt Likert agreement", "5-pt Likert agreement", "5-pt Likert agreement", "4-point closeness (Not at all close, not too close, somewhat close, very close)", "0-100 probability slider", "0-100 probability slider")

pilottable <- as.data.frame(cbind(questionlabels, questionscales))

#Calculating difference of means
diffs <- c(violent_t$estimate[2]-violent_t$estimate[1],
           trouble_t$estimate[2]-trouble_t$estimate[1],
           listen_t$estimate[2]-listen_t$estimate[1],
           justified_t$estimate[2]-justified_t$estimate[1],
           involved_t$estimate[2]-involved_t$estimate[1],
           close_t$estimate[2]-close_t$estimate[1],
           gotoprotest_t$estimate[2]-gotoprotest_t$estimate[1],
           socialmedia_t$estimate[2]-socialmedia_t$estimate[1]
) 

treats <- c(violent_t$estimate[2],trouble_t$estimate[2],listen_t$estimate[2],justified_t$estimate[2],involved_t$estimate[2],close_t$estimate[2],gotoprotest_t$estimate[2],socialmedia_t$estimate[2]) 
controls <- c(violent_t$estimate[1],trouble_t$estimate[1],listen_t$estimate[1],justified_t$estimate[1],involved_t$estimate[1],close_t$estimate[1],gotoprotest_t$estimate[1],socialmedia_t$estimate[1])
pvals <- c(violent_t$p.val, trouble_t$p.val, listen_t$p.val, justified_t$p.val, involved_t$p.val,close_t$p.val, gotoprotest_t$p.val, socialmedia_t$p.val)

pilottable$controls <- controls
pilottable$treats <- treats
pilottable$diffs <- diffs
pilottable$pvals <- pvals

colnames(pilottable) <- c("question_text", "question_scale", "ctrl_mean","trt_mean", "diff", "pvalue")
pilottable$pvalue <-p.adjust(pvals, method="BH")
print(xtable(pilottable, align="lp{3.75in}p{2.25in}llll", digits=c(0,0,0,2,2,2,2), caption="Outcome measures and differences-of-means from pilot study (adjusted p-values)"), include.rownames=FALSE, file="pilotests_dec2019_adjustedpvals.tex", floating.environment ='sidewaystable')
